from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

# 載入模型和分詞器
model = AutoModelForSequenceClassification.from_pretrained("jackietung/bert-base-chinese-sentiment-finetuned")
tokenizer = AutoTokenizer.from_pretrained("jackietung/bert-base-chinese-sentiment-finetuned")

# 準備輸入
text = "這個App使用體驗很差！"
inputs = tokenizer(text, return_tensors="pt")

# 進行預測
with torch.no_grad():
    outputs = model(**inputs)
    predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)

    # 獲取預測結果
    label_names = ["負面", "正面", "中性"]
    predicted_class = torch.argmax(predictions, dim=1).item()

    print(f"預測類別: {label_names[predicted_class]}")
    print(f"預測分數: {predictions[0][predicted_class].item():.4f}")

    # 顯示所有類別的分數
    for i, label in enumerate(label_names):
        print(f"{label} 分數: {predictions[0][i].item():.4f}")
